<?php
    namespace app\admin\model;
    use app\common\model\Model;
    use think\Db;
    
//  model 类
    class BatteryBanner extends Model
    {
        const TABLE_NAME = 'battery_banner';

        const BANNER_FIELDS= 'banner_id,title,banner_img,add_time,describe,is_show';

        const BANNER_IMAGE_PATH='battery_banner';
        protected $name = self::TABLE_NAME;

        public static function getBannerInfoById($id ,$field=''){
            if(!$id){
                return false;
            }

            if(!$field){
                $field=self::BANNER_FIELDS;
            }
            $info=self::commonQuery(self::TABLE_NAME) ->field($field)->where('banner_id',$id)->find();

            return $info ? $info :false;
        }


        // 显示和隐藏 banner
        public static function changeStatus($id=null,$status=null){

            if(is_null($id) || is_null($status)){
                return false;
            }

            $old_status=self::commonQuery(self::TABLE_NAME)->where('banner_id',$id)->column('is_show');

            if($old_status[0] != $status){
                $res = self::commonQuery(self::TABLE_NAME)->where('banner_id', $id)->update(['is_show' => $status]);

                if ($res) {
                    return true;
                }
            }

            return false;
        }

        public static function handleBannerInfo($info)
        {
            $info['banner_img']=get_image_url(self::BANNER_IMAGE_PATH,$info['banner_img']);
            return $info;
        }
        
        

}

